package com.example.bishe.web.c.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.bishe.entity.RecordChildren;
import com.example.bishe.service.RecordChildrenService;
import com.example.bishe.util.RestResult;
import com.example.bishe.web.c.dto.AddRecord;
import com.example.bishe.web.c.vo.BarVO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LDH
 * @since 2023/03/19
 */
@RestController
@RequestMapping("/c/record-children")
public class RecordChildrenController {

    @Autowired
    private RecordChildrenService service;

    @ApiOperation("分页查询用户二级记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "页大小", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "recordId", value = "一级记录id", paramType = "query"),
            @ApiImplicitParam(name = "time", value = "时间", paramType = "query"),
            @ApiImplicitParam(name = "isShow", value = "是否显示（0:否,1:是）", paramType = "query"),
    })
    @GetMapping("/page")
    public RestResult<IPage<AddRecord>> getPage(
            @RequestParam(required = false,defaultValue = "1")  int pageNum,
            @RequestParam(required = false,defaultValue = "10") int pageSize,
            @RequestParam(required = false) Long recordId,
            @RequestParam(required = false) String time,
            @RequestParam(required = false) Integer isShow
    ){
        return RestResult.get_success(service.gePage(pageNum,pageSize,recordId,time,isShow));
    }

    @ApiOperation("用户新建记录")
    @PostMapping("/add")
    public RestResult<String> add(@RequestBody AddRecord addRecord){
        int code = service.add(addRecord);
        switch (code) {
            case -2:
                return RestResult.error_msg(null,"当前有正在进行中的记录，无法新建",-2);
            case -1:
                return RestResult.error_msg(null,"已有当天日期的记录，无法新建",-1);
        }
        return RestResult.set_success("新建成功！");
    }

    @ApiOperation("用户编辑记录")
    @PostMapping("/update")
    public RestResult<String> update(@RequestBody AddRecord addRecord){
        int code = service.update(addRecord);
        if (code == -1)
            return RestResult.error_msg(null,"非当天记录，无法修改",-1);
        return RestResult.set_success("修改成功！");
    }

    @ApiOperation("用户删除记录")
    @PostMapping("/delete")
    public RestResult<String> delete(@RequestBody List<Long> ids){
        int code = service.delete(ids);
        return RestResult.set_success(code + "条记录未删除成功");
    }

    @ApiOperation("用户查询某标签内某月记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recordId", value = "标签id", paramType = "query"),
            @ApiImplicitParam(name = "year", value = "年份", paramType = "query"),
            @ApiImplicitParam(name = "month", value = "月份", paramType = "query"),
    })
    @GetMapping("/calendar")
    public RestResult<List<RecordChildren>> calendar(
            @RequestParam(required = false) Long recordId,
            @RequestParam(required = false) Integer year,
            @RequestParam(required = false) Integer month
    ){
        return RestResult.get_success(service.getCalendar(recordId,year,month));
    }

    @ApiOperation("用户查询条状统计图数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "页大小", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "recordId", value = "一级记录id", paramType = "query"),
            @ApiImplicitParam(name = "show", value = "是否显示（0:否,1:是）", paramType = "query"),
    })
    @GetMapping("/bar")
    public RestResult<List<BarVO>> getBarPage(
            @RequestParam(required = false,defaultValue = "1")  int pageNum,
            @RequestParam(required = false,defaultValue = "10") int pageSize,
            @RequestParam(required = false) Long recordId,
            @RequestParam(required = false) Integer show
    ){
        return RestResult.get_success(service.getBar(pageNum,pageSize,recordId,show));
    }

}
